package com.car.loan.common;

import com.car.loan.framework.web.domain.TreeSelect;
import com.car.loan.project.system.domain.SysDept;
import com.car.loan.project.system.domain.SysMenu;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author huangding
 * @description
 * @date 2019/11/5 14:55
 */
public class Recursions {


    public List<SysMenu> buildMenuTree(List<SysMenu> menus) {
        List<SysMenu> returnList = new ArrayList<SysMenu>();

        for (SysMenu t : menus) {
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == 0) {
                recursionFn(menus, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty()) {
            returnList = menus;
        }
        return returnList;
    }


    /**
     * 递归列表
     */
    private static void recursionFn(List<SysMenu> list, SysMenu t) {
        // 得到子节点列表
        List<SysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysMenu tChild : childList) {
            if (hasChild(list, tChild)) {
                // 判断是否有子节点
                Iterator<SysMenu> it = childList.iterator();
                while (it.hasNext()) {
                    SysMenu n = (SysMenu) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChild(List<SysMenu> list, SysMenu t) {
        return getChildList(list, t).size() > 0;
    }


    /**
     * 得到子节点列表
     */
    private static List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
        List<SysMenu> tlist = new ArrayList<SysMenu>();
        Iterator<SysMenu> it = list.iterator();
        while (it.hasNext()) {
            SysMenu n = (SysMenu) it.next();
            if (n.getParentId().longValue() == t.getMenuId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    public static <T> List<T> filter(List<T> list, Consumer<T> p) {
        List<T> result = new ArrayList<>();
        for (T e : list) {
            p.accept(e);
            result.add(e);
        }
        return result;
    }

    public static void main(String[] args) {

//        List<TreeSelect> treeSelects = Lists.newArrayList();
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(1L).setDeptName("1号部门").setParentId(0L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(2L).setDeptName("2号部门").setParentId(1L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(3L).setDeptName("3号部门").setParentId(2L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(4L).setDeptName("4号部门").setParentId(3L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(5L).setDeptName("5号部门").setParentId(4L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(6L).setDeptName("6号部门").setParentId(0L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(7L).setDeptName("7号部门").setParentId(6L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(8L).setDeptName("8号部门").setParentId(7L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(9L).setDeptName("9号部门").setParentId(8L)));
//        treeSelects
//            .add(new TreeSelect(new SysDept().setDeptId(10L).setDeptName("10号部门").setParentId(0L)));

        long l1 = System.currentTimeMillis();
        long l = f2(1L, 1L, 1000);
        System.out.println(l+"结果");
        System.out.println(System.currentTimeMillis() - l1);

        long l2 = System.currentTimeMillis();
        Recursion<Long> f = f(1L, 1L, 1000);
        Long result = f.result();
        System.out.println(result+"结果");
        System.out.println(System.currentTimeMillis() - l2);


        //获取一级，获取子集----
    }



    public static long f2(long l1, long l2, int count){
        if(count == 1){
            return l1;
        } else {
            return f2(l2, l1 + l2, --count);
        }

    }


    public static Recursion<Long> f(long l1, long l2, int count) {
        if (count == 1) {
            return done(l1);
        }
        return call(f(l2, l1 + l2, --count));
    }

    public static <T> Recursion<T> call(Recursion<T> recursion) {
        return recursion;
    }

    public static <T> Recursion<T> done(T result) {

        return new Recursion<T>() {
            @Override
            public Recursion<T> compute() {
                throw new UnsupportedOperationException("未实现的操作");
            }

            @Override
            public boolean isFinish() {
                return true;
            }

            @Override
            public T result() {
                return result;
            }
        };
    }

}
